<?php
/**
 * 反馈/客服消息
 * Created by PhpStorm.
 * User: poptser
 * Email: poptser@foxmail.com
 * Date: 2016/8/15
 * Time: 9:18
 */

namespace SDXApi\Model;


use Think\Model;

class FeedbackModel extends Model
{

    /**
     * 获取反馈/客服 消息列表
     * @param null $uid
     * @param int $current_page
     * @return bool
     */
    public function get_feedback_list($uid=null, $current_page=1){
        if(empty($uid)) return false;
        $collect_all = $this->where(array("user_id"=>$uid))->count();
        if($collect_all) {
            $return_data['sumcount'] = $collect_all;
            $all_page = ceil($collect_all / C("page_size"));
            $start = ($current_page-1) *C("page_size");
            $return_data['sumcount'] = $collect_all;
            if ($all_page == $current_page) {
                //判断是否为最后一页
                $return_data['is_bottom'] = 1;
            } else {
                $return_data['is_bottom'] = 0;
            }
            $feedback_data = $this->where(array("user_id"=>$uid))->limit($start,C("page_size"))->order("msg_time desc")->select();
            $return_data['list'] = $feedback_data;
            return $return_data;
        }else{
            return false;
        }
    }

    /**
     * 新加反馈/客服消息
     * @param int $type 类型  0，留言；1，投诉；2，询问；3，售后；4，求购
     * @param string $title  标题
     * @param string $content   内容
     * @param $uid      用户ID
     * @param int $order_id 该消息指定订单号  0 为不指定
     * @return bool
     */
    public function add_feedback($type=0, $title="my title", $content="my content", $uid, $order_id=0){
        if(empty($uid)) return false;
        $insert_data['user_id'] = $uid;
        $insert_data['user_name'] = D('Users')->get_user_username($uid);
        $insert_data['user_email'] = D('Users')->get_user_email($uid);
        $insert_data['msg_title'] = $title;
        $insert_data['msg_type'] = $type;
        $insert_data['msg_content'] = $content;
        $insert_data['msg_time'] = gmtime();
        $insert_data['order_id'] = $order_id;
        $insert_return = $this->add($insert_data);
        if($insert_return){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 删除客服/反馈消息
     * @param $uid
     * @param $feedback_id
     * @return bool
     */
    public function del_feedback($uid, $feedback_id){
        if (empty($uid)) return false;
        $where['user_id'] = $uid;
        $where['msg_id'] = $feedback_id;
        $del_return = $this->where($where)->delete();
        if($del_return){
            //删除成功  删除本类下面的小类
            $where_2['user_id'] = $uid;
            $where_2['parent_id'] = $feedback_id;
            $this->where($where_2)->delete();
            return true;
        }else{
            return false;
        }
    }

}